package zelix.hack.hacks;

import java.lang.reflect.Field;

import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.utils.BlockUtils;
import zelix.utils.system.Connection;
import zelix.value.BooleanValue;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.network.play.client.CPacketPlayerDigging;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.LeftClickBlock;
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
import zelix.utils.PlayerControllerUtils;
import zelix.utils.system.Mapping;
import zelix.utils.Wrapper;
import net.minecraftforge.fml.relauncher.ReflectionHelper;

public class FastBreak extends Hack {

	public BooleanValue Reflection;
	public FastBreak() {
		super("FastBreak", HackCategory.PLAYER);
		Reflection = new BooleanValue("Reflection",false);
		addValue(Reflection);
	}
	
	@Override
	public String getDescription() {
		return "Allows you to break blocks faster.";
	}
	
	@Override
	public void onClientTick(ClientTickEvent event) {
		PlayerControllerUtils.setBlockHitDelay(0);
		super.onClientTick(event);
	}
	
    @Override
    public void onLeftClickBlock(LeftClickBlock event) {
    	float progress = PlayerControllerUtils.getCurBlockDamageMP() + BlockUtils.getHardness(event.getPos());
    	if(progress >= 1) return;
    	Wrapper.INSTANCE.sendPacket(new CPacketPlayerDigging(
    			CPacketPlayerDigging.Action.STOP_DESTROY_BLOCK, event.getPos(),
    			Wrapper.INSTANCE.mc().objectMouseOver.sideHit));
    	super.onLeftClickBlock(event);
    }
	@Override
	public boolean onPacket(Object packet, Connection.Side side) {


		if (Reflection.getValue()){
			if(!Wrapper.INSTANCE.controller().isInCreativeMode()) {
				Field field = ReflectionHelper.findField(PlayerControllerMP.class, Mapping.curBlockDamageMP);
				Field blockdelay = ReflectionHelper.findField(PlayerControllerMP.class,Mapping.blockHitDelay);
				//Wrapper.INSTANCE.mc().playerController.blockHitDelay = 0;
				try {
					if (!field.isAccessible()) {
						field.setAccessible(true);
					}
					if (!blockdelay.isAccessible()) {
						blockdelay.setAccessible(true);
					}

					blockdelay.setInt(Wrapper.INSTANCE.controller(), 0);
					if (field.getFloat(Wrapper.INSTANCE.controller()) >= 0.7F)
					{
						field.setFloat(Wrapper.INSTANCE.controller(), 1F);
					}
				} catch (Exception e) {
					e.printStackTrace();

				}
			}
		}
		return true;
	}
	
}
